layui.config({
  base: '/' //假设这是你存放拓展模块的根目录
}).extend({ //设定模块别名
  murl: 'url' //如果 mymod.js 是在根目录，也可以不用设定别名
});
layui.use(['element','form','upload','layedit'], function () {
     var element = layui.element,
     					form = layui.form,
     					upload = layui.upload,
     					layedit = layui.layedit;
    layedit.set({
      uploadImage: {
        url: "/content/upload",
        type: 'post'
      }
    });
//    var index=layedit.build('editor_id', {
//       tool: ['strong' //加粗
//                ,'italic' //斜体
//                ,'underline' //下划线
//                ,'del' //删除线
//
//                ,'|' //分割线
//
//                ,'left' //左对齐
//                ,'center' //居中对齐
//                ,'right' //右对齐
//                ,'link' //超链接
//                ,'unlink' //清除链接
//                ,'face' //表情
//                ,'image' //插入图片
//                ,'code']
//     }); //建立编辑器
    var simplemde = new SimpleMDE({
        element: document.getElementById("editor_id"),
        autoDownloadFontAwesome: false,
        status: false,
         toolbar: [{
                    name: "bold",
                    action: SimpleMDE.toggleBold,
                    className: "fa fa-bold",
                    title: "Bold (Ctrl+B)",
                }
            ],
    });
     autoSelect("#types","category");
     autoSelect("#tags","tag");

     form.verify({
            content: function(value) {
                return layedit.sync(index);
            }
        });
     form.on('submit(save)', function(data){

        submitForm(data);
       return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
     });

     form.on('submit(cancel)', function(data){
        layer.confirm('确定取消吗？', null, function (index) {
            $('.layui-form')[0].reset();
             layer.close(index);
          });

        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
      });
});

function submitForm(data){
  $.ajax({
     url: '/content/edit',
     type: 'post',
     data: data.field,
     dataType: "json",
     success: function (data) {
         var layerTips = parent.layer === undefined ? layui.layer : parent.layer;
         if(data.rel){
            layerTips.msg('保存成功');

         }else{
           layerTips.msg(data.msg);
         }
     }
   });
}

function setLabelValues(data) {
    for (var p in data) {
        $("#" + p + "").html(data[p]);
    }
}

function setInputValues(data) {
    for (var p in data) {
        $("#" + p + "").val(data[p]);
    }
}

function autoSelect(element,code){
$(element).tokenfield({
       autocomplete: {
         source: function( request, response ) {
            $.ajax( {
              url: "/type/list?name="+ request.term+"&code="+code,
              dataType: 'json',
               delay: 100,
              success: function( data ) {
               var result = [];
                for (var i = 0; i < data.rows.length; i++) {
                    result.push(data.rows[i].name);
                }
                response(result);

              }
            } );
          },
         delay: 100
       },

       showAutocompleteOnFocus: true,
       createTokensOnBlur:true
     });
}
